package com.goldsprite.gameframeworks.ecs.component;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.goldsprite.gameframeworks.assets.FontUtils;
import com.goldsprite.gameframeworks.assets.MBitmapFont;
import com.goldsprite.gameframeworks.utils.math.Vector2;

public class WorldTxtComponent extends RenderableComponent {
	private MBitmapFont mfont;
	private GlyphLayout glyphLayout;
	private String text;
	public boolean isAlignCenter;

	public WorldTxtComponent() {
	}

	public void initFnt() {
		initFnt(FontUtils.defaultFntSize);
	}

	public void initFnt(int fntSize) {
		mfont = new MBitmapFont(fntSize);
		glyphLayout = new GlyphLayout();
	}

	public MBitmapFont getMFont() {
		return mfont;
	}

	public void setMFont(MBitmapFont mfont) {
		this.mfont = mfont;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
		glyphLayout.setText(mfont.font, text);
	}

	@Override
	public void render(Batch batch,  float delta) {
		//跳过未启用与未初始化文字
		if (!isEnable() || mfont == null) return;
		//绘制: 中心转左下原点坐标, 计算拉伸
		Vector2 leftDownPos = getLeftDownPos();
		if (isAlignCenter) {
			float cx = leftDownPos.x - glyphLayout.width / 2;
			float cy = leftDownPos.y + glyphLayout.height / 2;
			mfont.font.draw(batch, text, cx, cy);
		} else {
			mfont.font.draw(batch, text, leftDownPos.x, leftDownPos.y);
		}
	}
}
